<?php
/**
 * 企业资料model
 * User: guojing
 * Date: 2020/4/5
 * Time: 21:54
 */
namespace app\api\model;

use think\Db;
use think\Log;
use think\Model;

class Project extends Model
{

    protected $name = 'project';

    public static function findInfoByCondition($condition = [])
    {
        return self::where($condition)->find();
    }

    /**
     * 获取列表
     */
    public static function dataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['puid'])) {
            $where['puid'] = $params['puid'];
        }

        $list = self::where($where)->page($page)->order('id desc')->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                // 周期
                $cycleDay = (int)((strtotime($listVal['enddate']) - strtotime($listVal['startdate'])) / (24 * 3600));
                $cycleDay < 0 && $cycleDay = 0;
                // 剩余天数
                $remainingDay = (int)((strtotime($listVal['enddate']) - time()) / (24 * 3600));
                $remainingDay < 0 && $remainingDay = 0;
                $data = [
                    'pname' => $listVal['pname'],
                    'pcontent' => $listVal['pcontent'],
                    'cycle_days' => $cycleDay,
                    'team_numbers' => ProjectAssign::projectAssignNums($listVal['id']),
                    'remaining_day' => $remainingDay,
                    'id' => $listVal['id'],
                    'spend_progress' => $listVal['spend_progress'] . '%',
                    'is_end' => (int)$listVal['is_end']
                ];
                $listData[] = $data;
                unset($data);
            }
        }
        return $listData;
    }

    public static function addRecord($addData = [])
    {
        return self::create($addData);
    }

    public static function cdataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['uid'])) {
            $where['pa.uid'] = $params['uid'];
        }
        $list = db('project')->alias('p')->join('project_assign pa', 'p.id = pa.project_id', 'inner')->where($where)->field('p.*')->group('p.id')->order('p.id desc')->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                // 周期
                $cycleDay = (int)((strtotime($listVal['enddate']) - strtotime($listVal['startdate'])) / (24 * 3600));
                $cycleDay < 0 && $cycleDay = 0;
                // 剩余天数
                $remainingDay = (int)((strtotime($listVal['enddate']) - time()) / (24 * 3600));
                $remainingDay < 0 && $remainingDay = 0;
                $data = [
                    'pname' => $listVal['pname'],
                    'pcontent' => $listVal['pcontent'],
                    'cycle_days' => $cycleDay,
                    'team_numbers' => ProjectAssign::projectAssignNums($listVal['id']),
                    'remaining_day' => $remainingDay,
                    'id' => $listVal['id'],
                    'is_end' => (int)$listVal['is_end']
                ];
                $listData[] = $data;
                unset($data);
            }
        }
        return $listData;
    }

    /**
     * 更新数据
     */
    public static function saveData($contidion = [], $data)
    {
        if ($contidion && $data) {
            return self::where($contidion)->update($data);
        }
    }

}